package fi.helsinki.cs.kaisei;

/**
 * @author Team TA's
 */

import java.io.Serializable;
import java.util.ArrayList;
import java.util.Iterator;

public class Schedule implements Serializable {
        
        private static final long serialVersionUID = -713498738200277480L;
        private String period;
    private ArrayList<Timetable> timetables;

    // CONSTRUCTORS
    
    // this constructor is solely for mock objects to use, hence protected
    protected Schedule() { 
        this.period = null;
        this.timetables = new ArrayList<Timetable>();
    }
    
    public Schedule(String period) {
        this.timetables = new ArrayList<Timetable>();
        this.period = period;
    }
    
    public Schedule(ArrayList<Timetable> timetables, String period) {
        this.timetables = timetables;
        this.period = period;
    }

    // GETTERS AND SETTERS
    
    public void setSchedule(Schedule schedule) {
        this.timetables = schedule.timetables;
        this.period = schedule.period;
    }
    
    public ArrayList<Timetable> getTimetables() {
        return timetables;      
    }
    
        public String getPeriod() {
                return period;
        }

        public void setPeriod(String period) {
                this.period = period;
        }
    
    // OTHER
        public void addTimetable(Timetable timetable) {
                timetables.add(timetable);
        }

    public ArrayList<Timetable> searchTimetables(int startWeek, int endWeek) {
            if (startWeek < 1 || startWeek > 52 || endWeek < 1 || endWeek > 52)
                throw new IllegalArgumentException("Week numbers must be between 1-52");
        
        ArrayList<Timetable> returnTable = new ArrayList<Timetable>();        
        for (Iterator<Timetable> it = timetables.iterator(); it.hasNext();) {
               Timetable timetable = (Timetable) it.next();
               if (startWeek > endWeek) {
                   if (timetable.getStartWeek() <= endWeek || timetable.getEndWeek() >= startWeek)
                       returnTable.add(timetable);
                   else if (timetable.getStartWeek() > timetable.getEndWeek()) {
                       if (timetable.getStartWeek() <= startWeek || timetable.getEndWeek() >= endWeek)
                           returnTable.add(timetable);
                       else if (timetable.getStartWeek() >= startWeek && timetable.getEndWeek() <= endWeek)
                           returnTable.add(timetable);
                   }
               }
               else {
                   if (timetable.getStartWeek() >= startWeek && timetable.getStartWeek() <= endWeek)
                           returnTable.add(timetable);
                   else if (timetable.getEndWeek() >= startWeek && timetable.getEndWeek() <= endWeek)
                           returnTable.add(timetable);
                   else if (timetable.getStartWeek() <= startWeek && timetable.getEndWeek() >= endWeek)
                           returnTable.add(timetable);
               }
        }       
        return returnTable;
    }
    
}
